********************DATA PREP********************
cd "C:\RCFS Data"
clear all
set more off
use "Compustat data.dta", clear
 

*****SIMULATIONS*************************************************************************
*Keep only variables needed for loop*
keep gvkey year sic2 roa roe logat logme td2ta
drop if roa==. & roe==.
drop if logat==. & logme==.

save "Dataset_p.dta", replace

tempname memhold
postfile `memhold' sim coeff se obs expvarsd expvarp25 expvarp75 depvarsd depvarmean alin dvmo scat cvin evlg olin using "SIXout_p ffe.dta", replace

*Create Variables for loop*
	forval sim=1/100 {
	use "Dataset_p.dta", clear
	
	*Create a normally distributed variable
	gen newmean=runiform(10,100)
	gen newstdev=runiform(1,10)
	gen expvar=rnormal(newmean[1],newstdev[1])
	drop newmean newstdev

	qui gen depvar=.
	qui gen sizecontrol=.

save "Workspace_p.dta", replace

	forval alin=0/1 {
	forval dvmo=0/1 {
	forval scat=0/1 {
	forval cvin=0/1 {
	forval evlg=0/1 {
	forval olin=0/1 {
	
	use "Workspace_p.dta", clear
	
	*1. SAMPLE SELECTION*****************************************
	
	*Industries
	qui if `alin'==0 drop if sic2>59 & sic2<70 /* Drop financial firms*/
	* when alin==1, uses all industries
	
	*2. VARIABLE SELECTION*****************************************
	*Choose Dependent Variable
	qui if `dvmo'==1 replace depvar=roa  
	qui if `dvmo'==0 replace depvar=roe 
	qui drop if depvar==.
	drop roa roe
	
	*Choose Size Control
	qui if `scat'==1 replace sizecontrol=logat
	qui if `scat'==0 replace sizecontrol=logme
	qui drop if sizecontrol==.
	drop logat logme
	
	*Add Other Controls (executed below in regression)
	

	*3. VARIABLE TRANSFORMATION*****************************************
	
	*Lag Explanatory Variable
	if `evlg'==1 {
	sort gvkey year
	qui gen expvarlag=expvar[_n-1] if gvkey==gvkey[_n-1] & year==year[_n-1]+1
	qui drop if expvarlag==.
	drop expvar
	rename expvarlag expvar
	}


	*Outliers
	* when olin=1, retain outliers
	
	if `olin'==0 winsor2 depvar expvar sizecontrol td2ta, replace cuts(1 99)
	
	qui summ expvar, detail
	gen expvarsd=r(sd)
	gen expvarp25=r(p25)
	gen expvarp75=r(p75)
	qui summ depvar
	gen depvarsd=r(sd)
	gen depvarmean=r(mean)
	
	qui if `cvin'==0 areg depvar expvar sizecontrol i.year, absorb(gvkey) cluster(gvkey)
	qui if `cvin'==1 areg depvar expvar sizecontrol td2ta i.year, absorb(gvkey) cluster(gvkey)
	
	gen coeff=_b[expvar]
	gen se=_se[expvar]
	gen obs=e(N)
	
	post `memhold' (`sim') (coeff) (se) (obs) (expvarsd) (expvarp25) (expvarp75) (depvarsd) (depvarmean) (`alin') (`dvmo') (`scat') (`cvin') (`evlg') (`olin')

    }
	}
	}
	}
	}
	}
	di "simulation_p "`sim'
	drop depvar expvar sizecontrol coeff se obs
	}

postclose `memhold'

